<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
class SanPhamModelDetail extends JModel{
	var $_id = null;
	var $_data = null;
	
	function __construct(){
		parent::__construct();
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$edit	= JRequest::getVar('edit',true);
		if($edit)
			$this->setId((int)$array[0]);
	}
	function setId($id){
		$this->_id		= $id;
		$this->_data	= null;
	}
	function &getData(){		
		if ($this->_loadData()){
		}else{
			$this->_initData();
		}
		//prepare_process::prepareLoad(&$this->_data);
		return $this->_data;
	}
	function delete($cid = array()){		
		$db =& JFactory::getDBO();
		$result = false;
		if(count($cid)){			
			foreach($cid as $key=>$value){	
				$query = "SELECT * FROM #__product WHERE product_id =".$value;
				$db->setQuery($query);
				$row = $db->loadObject();
				//prepare_process::prepareDelete(&$row);
				$query = "DELETE FROM #__product WHERE product_id =".$value;					
				$db->setQuery($query);
				if(!$db->query()){
					die($this->_db->getErrorMsg());
				}				
			}
		}		
		return true;		
	}
	function frontpage($cid = array()){		
		$db =& JFactory::getDBO();
		$result = false;
		if(count($cid)){			
			foreach($cid as $key=>$value){				
				$query = "UPDATE #__product set frontpage='1' WHERE product_id =".$value;					
				$db->setQuery( $query );
				if (!$db->query())
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}				
			}
		}		
		return true;		
	}
	function publish($cid = array(), $publish = 1){
		if (count( $cid )){
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'UPDATE #__product'
				. ' SET published = '.(int) $publish
				. ' WHERE product_id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}
	/*function publishSoldOut($cid = array(), $publishsoldout = 1){
		if (count( $cid )){
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'UPDATE #__product'
				. ' SET saleyesno = '.(int) $publishsoldout
				. ' WHERE product_id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}
	function getFontpage($id){		
		$db =&JFactory::getDBO();
		$query = 'select * from #__homepage where content_id = '.$id;		
		$db->setQuery($query);
		$row = $db->loadResult();
		if(count($row)){
			return 1;	
		}
		return 0;
	}*/
	
	function store($data){
		$row =& $this->getTable('sanpham');
		if (!$row->bind($data)) {
			die($this->_db->getErrorMsg());
		}if (!$row->product_id) {
			$where = 'parent = ' . (int) $row->parent ;
			$row->ordering = $row->getNextOrder( $where );
		}	
		//prepare_process::prepareSave(&$row);
		if (!$row->store()) {
			die($this->_db->getErrorMsg());
			
		}
		return true;
	}
	function move($direction){
		$row =& $this->getTable('SanPham');
		if (!$row->load($this->_id)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->move( $direction, ' parent = '.(int) $row->parent.' AND published >= 0 ' )) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}
	function saveorder($cid = array(),$order){
		$row =& $this->getTable('SanPham');
		$groupings = array();
		for( $i=0; $i < count($cid); $i++ ){
			$row->load( (int) $cid[$i] );
			// track categories
			$groupings[] = $row->parent;
			if ($row->ordering != $order[$i]){
				$row->ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		$groupings = array_unique( $groupings );
		foreach ($groupings as $group){
			$row->reorder('parent = '.(int) $group);
		}
		return true;
	}
	function _loadData(){
		// Lets load the content if it doesn't already exist
		if (empty($this->_data)){
			$query = 'SELECT * FROM #__product'.
					' WHERE product_id = '.(int) $this->_id;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
		}
		return true;
	}
	function _initData(){
		// Lets load the content if it doesn't already exist
		if (empty($this->_data)){
			$sp = new stdClass();
			$sp->product_id		= 0;
			$sp->title			= null;
			$sp->ordering		= null;
			$sp->parent			= null;
			$sp->published     	= 1;
			$sp->description 	= null;
			$sp->mtype 			= null;
			$sp->price			=null;
			$sp->image			=null;
			$sp->created 		= null;
			$sp->quantity 		= null;
			$sp->frontpage 		= null;
			$this->_data 		= $sp;
			return (boolean) $this->_data;
		}
		return true;
	}
}